package com.mantou.scucorse.core.scheduler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.mantou.scucorse.core.entity.User;
import com.mantou.scucorse.core.service.UserService;
import com.mantou.scucorse.utils.UserSatus;

public class UserAuthor implements Runnable {

	@Autowired
	private UserService userService;
	
	@Autowired
	private CourseNetInvoker courseNetInvoker;
	
	private ThreadPoolTaskExecutor executor;
	
	public ThreadPoolTaskExecutor getExecutor() {
		return executor;
	}

	public void setExecutor(ThreadPoolTaskExecutor executor) {
		this.executor = executor;
	}

	public void run() {
		// TODO Auto-generated method stub

		List<User> users = userService.find2Auth();
		for(User user :users){
			executor.execute(new InitExecuter(user,userService,courseNetInvoker));
		}
	}

	private class InitExecuter implements Runnable{

		private User user;
		private UserService userService;
		private CourseNetInvoker courseNetInvoker;
		
		public InitExecuter(User user, UserService userService,
				CourseNetInvoker courseNetInvoker) {
			super();
			this.user = user;
			this.userService = userService;
			this.courseNetInvoker = courseNetInvoker;
		}

		public void run() {
			// TODO Auto-generated method stub
		/*	int i=0;
			if(i==0){
				return;
			}*/
			int loginstatus = courseNetInvoker.login(user.getId());
			if(0 ==loginstatus){
				user.setStatus(UserSatus.ONLNE.getStatus());
			}else if(1 == loginstatus){
				user.setStatus(UserSatus.PASSWORDERROR.getStatus());
			}
			
			userService.updateByPrimaryKey(user);
		}
		
	}
}
